<?php

/**
 * User Model
 * Database functions related to user table
 * 
 * @project : Loobica
 * @author  : Dino
 * @date    : 18 Jan 2014
 * 
 */
class User_model extends CI_Model {

  function __construct() {
    parent::__construct();
  }

  /**
   * Insert user
   * 
   * @param type $user_data
   * @return type
   */
  function put_user($user_data = array()) {
    $this->db->insert('user', $user_data);
    return (int) $this->db->insert_id();
  }

  /**
   * Update user
   * 
   * @param type $user_data
   * @param type $user_id
   * @return type
   */
  function set_user($user_data = array(), $user_id = null) {
    $this->db->update('user', $user_data, array('user_id' => (int) $user_id));
    return (int) $this->db->affected_rows();
  }

  /**
   * Get user by email
   * 
   * @param type $email
   * @param type $status
   * @param type $all
   * @return type
   */
  function get_email($email = null, $status = null, $all = false) {
    if ($all) {
      $select = '*';
    } else {
      $select = 'user_id, user_email, user_status';
    }
    $where = array();
    $where['user_email'] = (string) $email;
    if ($status) {
      $where['user_status'] = (int) $status;
    }
    return $this->db->select($select)
                    ->where($where)
                    ->get('user')
                    ->row_array();
  }

  /**
   * Get user by id
   * 
   * @param type $user_id
   * @param type $status
   * @param type $all
   * @return type
   */
  function get_user_id($user_id = null, $status = null, $all = false) {
    if ($all) {
      $select = '*';
    } else {
      $select = 'user_id, user_email, user_status';
    }
    $where = array();
    $where['user_id'] = (int) $user_id;
    if ($status) {
      $where['user_status'] = (int) $status;
    }
    return $this->db->select($select)
                    ->where($where)
                    ->get('user')
                    ->row_array();
  }

}

?>